<?php

/***
 * @author: sa0ChunLuyu
 * @time: 2019-09-17 13:14
 */
class slidingBlockVerifyCode
{
    private $background_width;
    private $background_height;
    private $jigsaw_width;
    private $jigsaw_height;
    private $image_dir;
    private $jigsaw_path;

    public function __construct($background_width, $background_height, $jigsaw_width, $jigsaw_height, $image_dir, $jigsaw_path)
    {
        $this->background_width = $background_width;
        $this->background_height = $background_height;
        $this->jigsaw_width = $jigsaw_width;
        $this->jigsaw_height = $jigsaw_height;
        $this->image_dir = $image_dir;
        $this->jigsaw_path = $jigsaw_path;
    }

    public function create()
    {
        $image_path = self::__getImagePath($this->image_dir);
        list($image_width, $image_height, $image_type) = getimagesize($image_path);
        $data_ = [];
        $background_width = $this->background_width;
        $background_height = $this->background_height;
        $data_['background_width'] = $background_width;
        $data_['background_height'] = $background_height;
        if ($image_width / $image_height > $background_width / $background_height) {
            $image_true_width = $image_width / $image_height * $background_height;
            $image_true_height = $background_height;
        } else {
            $image_true_width = $background_width;
            $image_true_height = $image_height / $image_width * $background_width;
        }
        $image_types = array(1 => "gif", 2 => "jpeg", 3 => "png",);
        $create_from = "imagecreatefrom" . $image_types[$image_type];
        $background_image = $create_from($image_path);
        $background = imagecreatetruecolor($background_width, $background_height);
        imagecopyresampled($background, $background_image, ($image_true_width - $background_width) * -1 / 2, ($image_true_height - $background_height) * -1 / 2, 0, 0, $image_true_width, $image_true_height, $image_width, ($image_true_height / $image_true_width) * $image_width);
        $jigsaw_width = $this->jigsaw_width;
        $jigsaw_height = $this->jigsaw_height;
        $data_['jigsaw_width'] = $jigsaw_width;
        $data_['jigsaw_height'] = $jigsaw_height;
        $jigsaw_x = rand($jigsaw_width, $background_width - $jigsaw_width);
        $jigsaw_y = rand(0, $background_height - $jigsaw_height);
        $data_['jigsaw_x'] = $jigsaw_x;
        $data_['jigsaw_y'] = $jigsaw_y;
        $jigsaw = imagecreatetruecolor($jigsaw_width, $jigsaw_height);
        imagecopyresampled($jigsaw, $background, 0, 0, $jigsaw_x, $jigsaw_y, $background_width, $background_height, $background_width, $background_height);
        ob_start();
        imagejpeg($jigsaw);
        imagedestroy($jigsaw);
        $j = ob_get_clean();
        $data_['jigsaw_base64'] = base64_encode($j);
        list($jigsaw_file_width, $jigsaw_file_height, $jigsaw_type) = getimagesize($this->jigsaw_path);
        $create_from = "imagecreatefrom" . $image_types[$jigsaw_type];
        $jigsaw_image = $create_from($this->jigsaw_path);
        imagecopyresampled($background, $jigsaw_image, $jigsaw_x, $jigsaw_y, 0, 0, $jigsaw_width, $jigsaw_height, $jigsaw_file_width, $jigsaw_file_height);
        ob_start();
        imagejpeg($background);
        imagedestroy($background);
        $b = ob_get_clean();
        $data_['background_base64'] = base64_encode($b);
        return $data_;
    }

    public function __getImagePath($image_dir)
    {
        $image_path_array = [];
        $index_ = 0;
        foreach (scandir($image_dir) as $name) if ($name !== '.' && $name !== '..' && in_array(pathinfo($name, PATHINFO_EXTENSION), ['jpg', 'jpeg'])) {
            $image_path_array[$index_] = $name;
            $index_++;
        }
        return $image_dir . $image_path_array[rand(0, count($image_path_array) - 1)];
    }
}